<template>
  <div>
    <el-card id="search">
      <el-row>
        <el-col :span="4">
          <el-button
            @click="openEditUI(null)"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <el-table :data="amountList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="sendTime" label="发放时间" width="150">
        </el-table-column>
        <el-table-column prop="teacher" label="负责人" width="150">
        </el-table-column>
        <el-table-column prop="postNum" label="岗位人数" width="100">
        </el-table-column>
        <el-table-column prop="sumMoney" label="总金额"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" size="mini"
                       circle></el-button>
            <el-button @click="deleteSend(scope.row)" type="danger" icon="el-icon-delete" size="mini"
                       circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
<!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog
      @close="clearForm"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="amountForm" ref="amountFormRef" :rules="rules">
        <el-form-item
          label="日期"
          prop="sendTime"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="amountForm.sendTime"
            type="month"
            placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="老师"
          prop="teacher"
          :label-width="formLabelWidth"
        >
          <span class="demonstration">{{amountForm.teacher}}</span>
        </el-form-item>
        <el-form-item
          label="岗位"
          prop="post"
          :label-width="formLabelWidth"
        >
          <el-select :disabled="postDisable"  v-model="amountForm.postId" @change="changePost($event)" placeholder="请选择岗位">
            <el-option
              v-for="item in postList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-table
          :data="amountForm.list"
          stripe
          style="width: 100%">
          <el-table-column
            prop="student"
            label="学生姓名"
            width="180">
          </el-table-column>
          <el-table-column
            prop="money"
            label="金额">

            <template slot-scope="scope">
              <el-input v-model="scope.row.money" placeholder="请输入金额"></el-input>
            </template>

          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import postApi from "@/api/post";
import sendMoneyApi from "@/api/sendMoney";
import userApi from "@/api/userManage"
import moment from 'moment';

export default {
  data() {
    return {
      title: "",
      total: 0,
      formLabelWidth: "130px",
      searchModel: {
        pageNo: 1,
        pageSize: 10
      },
      postDisable: false,
      amountList: [],
      postList: [],
      amountForm: {
        sendTime: "2023-01",
        teacher: "me"
      },
      rules: {

      },
      dialogFormVisible: false,
    }
  },
  methods: {
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getPostList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getList();
    },
    getList() {
      sendMoneyApi.getSendMoneyList(this.searchModel).then((response) => {
        this.amountList = response.data.records
        this.total = response.data.total
      })
    },
    save() {
      this.amountForm.sendTime = moment(this.amountForm.sendTime).format('YYYY-MM-01')
      sendMoneyApi.save(this.amountForm).then(response => {
        // 成功提示
        this.$message({
          message: response.message,
          type: 'success'
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 刷新表格
        this.getList()
      })
    },
    changePost(value) {
      postApi.getStudentByPostId(value).then((response) => {
        let list = response.data.map(i => {
          return {
            studentId: i.id,
            student: i.name,
          }
        })
        this.$set(this.amountForm, 'list', list)
      })
    },
    clearForm() {
      this.amountForm = {};
      this.$refs.amountFormRef.clearValidate();
    },
    deleteSend(send) {
      this.$confirm(`您确认删除数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sendMoneyApi.deleteById(send.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    openEditUI(id) {
      this.dialogFormVisible = true;
      if (id == null) {
        this.title = "发放金额";
        let token = this.$store.state.user.token
        userApi.getInfo(token).then(res => {
          this.$set(this.amountForm, 'teacherId', res.data.id)
          this.$set(this.amountForm, 'teacher', res.data.name)
        })
        this.postDisable = false
      } else {
        this.title = "发放金额";
        // 根据id查询用户数据
        sendMoneyApi.getById(id).then(response => {
          this.amountForm = response.data;
          let moneyMap = {}
          response.data.list.forEach(i => {
            moneyMap[i.studentId] = i;
          })
          postApi.getStudentByPostId(this.amountForm.postId).then((response) => {
            let list = response.data.map(i => {
              return {
                studentId: i.id,
                student: i.name,
                money: moneyMap[i.id] ? moneyMap[i.id].money : undefined
              }
            })
            this.$set(this.amountForm, 'list', list)
          })  
        });
        
        this.postDisable = true
      }

      postApi.allPost().then(response => {
        this.postList = response.data
      })
    },
  },
  created() {
    this.getList()
  }
}
</script>

<style>

</style>
